/**
 * Created by x on 17-7-12.
 */
angular.module('tribalc').controller(
    'apartmentInfoController',
    ['$rootScope', '$scope', '$state', '$stateParams', 'Api','$location','Alert','ApartmentInfoModel',
        function ($rootScope, $scope, $state, $stateParams, Api,StaffModel,$location,Alert,ApartmentInfoModel) {

            $scope.showCompanyEdit=showCompanyEdit;
            $scope.apartmentId = $stateParams.apartmentId;

            getApartmentListInfo();
            $scope.isTitle=false;
            $scope.showApamentDelete=false;

            $scope.clickDelete=clickDelete;
            $scope.CheckIn=CheckIn;
            $scope.showPreImage = showPreImage;
            $scope.showNextImage = showNextImage;
            $scope.showCurrentPicture = showCurrentPicture;

            $scope.pictureNameInfo = {
                'Wi-Fi':'Wi-Fi',
                '书柜':'bookcase',
                '书桌':'desk',
                '床':'bed',
                '微波炉':'microwave',
                '智能锁':'intellock',
                '洗衣机':'washmachine',
                '热水器':'waterheater',
                '空调':'aircondition'
            };

            var step = 0;
            function showPreImage(element){

                if (step>0)
                {
                    step --;
                    $('#big-pro-picture')[0].setAttribute('src',$scope.ApamentListInfo.pictures[step]);

                    if ($scope.ApamentListInfo.pictures.length > 4 && step == 0)
                    {
                        $(element).parent().find('.image-container').animate({top:'+=88px'},300,function(){
                        })
                    }
                }
            }
            function showCurrentPicture(picture) {
                $('#big-pro-picture')[0].setAttribute('src',picture);
            }

            function showNextImage(element) {
                if ($scope.ApamentListInfo.pictures.length > step+1)
                {
                    step ++;
                    $('#big-pro-picture')[0].setAttribute('src',$scope.ApamentListInfo.pictures[step]);

                    if ($scope.ApamentListInfo.pictures.length > 4 && step == 4)
                    {
                        $(element).parent().find('.image-container').animate({top:'-=88px'},300,function(){
                        })
                    }
                }
            }

            function getApartmentListInfo() {
                Api.getCompanyDetail(null,function (result) {

                    Api.getApartmentListInfo(result.data.communityId,$scope.apartmentId,function (apartmentInfo) {
                        $scope.ApamentListInfo=apartmentInfo.data;
                        $scope.apamentStatus = apartmentInfo.data.status;
                        $rootScope.apamentInfo = apartmentInfo.data;
                        $scope.ApamentListInfo.pictures = $scope.ApamentListInfo.pictures?getReallyPicturePaths($scope.ApamentListInfo.pictures):null;

                        $scope.FloorHeight = apartmentInfo.data.floor + '/' + apartmentInfo.data.buildingHeight+ '层';
                    });
                });
            }



            function clickDelete() {
                $scope.showApamentDelete=true;

            }

            function getReallyPicturePaths(pictures) {
                if (!pictures) return [];
                Array.from(pictures).forEach(function (path, index) {
                    pictures[index] = formatReallyPicturePath(path);
                });
                return pictures;
            }

            function formatReallyPicturePath(path) {

                var oss = 'oss://';
                if (path && path.indexOf(oss) !== -1) {
                    path = Api.ALIYUN_URL + path.split(oss)[1];
                }
                return path;
            }

            function CheckIn( ) {
                $scope.isTitle='true'
                var id=$scope.ApamentListInfo.id;
                $scope.ApamentListInfo.status='CHECK';
                Api.perfectApartmentInfo($scope.ApamentListInfo.status,id,function (result) {
                    // console.log(result);
                })
            }


            function showCompanyEdit(content){
                $state.go('company.companyamendApartmentInfo',{apartmentId:content});
            }

        }]);